package com.suneee.store.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.context.page.Page;
import com.suneee.context.page.Sort;
import com.suneee.context.page.Sort.Order;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.store.bean.ProductDTO;
import com.suneee.store.dao.ShopStockDao;
import com.suneee.store.entity.ShopStock;

@Repository("shopStockDao")
public class ShopStockDaoImpl extends GenericDao<ShopStock, Integer>implements ShopStockDao {

	@Override
	public ShopStock selectByIdEnterpriseCode(Integer stockId, String enterpriseCode) {
		// TODO Auto-generated method stub
		String sql = "select ss.* from t_shop_stock ss,t_shop s "
				+ "where ss.shop_id = s.shop_id and ss.stock_id = ? and s.enterprise_code = ?";
		Object[] args = new Object[2];
		args[0] = stockId;
		args[1] = enterpriseCode;
		List<ShopStock> shopStockList = this.executeQuery(sql, args);
		if(shopStockList == null || shopStockList.isEmpty())
			return null;
		return shopStockList.get(0);
	}

	@Override
	public ShopStock selectBySkuIdEnterpriseCode(Integer skuId, String enterpriseCode) {
		// TODO Auto-generated method stub
		String sql = "select ss.* from t_shop_stock ss,t_shop s "
				+ "where ss.shop_id = s.shop_id and ss.sku_id = ? and s.enterprise_code = ?";
		Object[] args = new Object[2];
		args[0] = skuId;
		args[1] = enterpriseCode;
		List<ShopStock> shopStockList = this.executeQuery(sql, args);
		if(shopStockList == null || shopStockList.isEmpty())
			return null;
		return shopStockList.get(0);
	}

	@Override
	public List<ShopStock> getListBySkuId(Integer skuId, String enterpriseCode) {
		// TODO Auto-generated method stub
		String sql = "select ss.* from t_shop_stock ss,t_shop s "
				+ "where ss.shop_id = s.shop_id and ss.sku_id = ? and s.enterprise_code = ?";
		Object[] args = new Object[2];
		args[0] = skuId;
		args[1] = enterpriseCode;
		return this.executeQuery(sql, args);
	}

	@Override
	public ShopStock getListBySkuSpu(Integer spuId, Integer skuId,String enterpriseCode,Integer shopId) {
		String sql = "select ss.* from t_shop_stock ss,t_shop s "
				+ "where ss.shop_id = s.shop_id and ss.spu_id = ? "
				+ "and ss.sku_id = ? and s.enterprise_code = ? and s.shop_id = ? ";
		Object[] args = new Object[]{spuId,skuId,enterpriseCode,shopId};
		List<ShopStock> shopStockList = this.executeQuery(sql, args);
		if(shopStockList == null || shopStockList.isEmpty())
			return null;
		return shopStockList.get(0);
	}

	@Override
	public ShopStock getByShopIdSkuIdEpCode(Integer skuId, String enterpriseCode, Integer shopId) {
		String sql = "select ss.* from t_shop_stock ss,t_shop s "
				+ "where ss.shop_id = s.shop_id and ss.sku_id = ? and ss.shop_id=? and s.enterprise_code = ?";
		Object[] args = new Object[3];
		args[0] = skuId;
		args[1] = shopId;
		args[2] = enterpriseCode;
		List<ShopStock> shopStockList = this.executeQuery(sql, args);
		if(shopStockList == null || shopStockList.isEmpty())
			return null;
		else 
			return shopStockList.get(0);
	}

	@Override
	public Page<Map<String, Object>> mapView(ProductDTO dto) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT stock.sku_id as skuId,spu.title as title,stock.create_time as createTime,stock.price,");
		sql.append("stock.stock FROM t_shop_stock AS stock INNER JOIN t_spu AS spu ON stock.spu_id = spu.spu_id ");
		WhereClauseBuilder sbf = new WhereClauseBuilder(sql.toString());
		sbf.equals("spu.enterprise_code", dto.getEnterpriseCode());
		sbf.equals("stock.sku_id", dto.getSkuId());
		sbf.contains("spu.title", dto.getTitle());
		sbf.append(" and stock.stock > ? ", 0);
		sbf.equals("shop_id", dto.getShopId());
		dto.setSort(new Sort("spu.spu_id",Order.DESC));
		return this.queryForList(sbf.getSql(), dto, sbf.getArgs());
	}

}
